deliveries.js ➔ addDelivery   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 25
ccs 5
cts 5
cp 1
rs 9.45
c 0
b 0
f 0
cc 3
crap 3
1 1
const db = require("../db/database.js");
2
3 1
module.exports = (function () {
4
    function getDeliveries(res, apiKey) {
5 2
        const sql = "SELECT deliveryId as id, productId as product_id, amount," +
6
                        " deliveryDate as delivery_date, comment" +
7
                        " FROM deliveries WHERE apiKey = ?";
8
9 2
        db.all(sql, apiKey, (err, rows) => {
10 2
            if (err) {
11
                return res.status(500).json({
12
                    errors: {
13
                        status: 500,
14
                        source: "/deliveries",
15
                        title: "Database error",
16
                        detail: err.message
17
                    }
18
                });
19
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
20 2
                res.json({ data: rows });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
21
            }
22
        });
23
    }
24
25
    function addDelivery(res, body) {
26 6
        const sql = "INSERT INTO deliveries (deliveryId, productId, amount, deliveryDate," +
27
                        " comment, apiKey) VALUES (?, ?, ?, ?, ?, ?)";
28
29 6
        db.run(sql,
30
            body.id,
31
            body.product_id,
32
            body.amount,
33
            body.delivery_date,
34
            body.comment,
35
            body.api_key, (err) => {
36 6
                if (err) {
37 5
                    return res.status(500).json({
38
                        errors: {
39
                            status: 500,
40
                            source: "/delivery",
41
                            title: "Database error",
42
                            detail: err.message
43
                        }
44
                    });
45
                } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
46 1
                    res.status(201).json({ data: body });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
47
                }
48
            });
49
    }
50
51 1
    return {
52
        getDeliveries: getDeliveries,
53
        addDelivery: addDelivery,
54
    };
55
}());
56